package com.zhongketech.sunflower.filter;

import java.util.ArrayList;
import java.util.List;

import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.database.AppInfo;
import com.zhongketech.sunflower.database.ApplicationData;
import com.zhongketech.sunflower.database.Data;
import com.zhongketech.sunflower.util.Constants;
import com.zhongketech.sunflower.util.Utilities;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class AppManager {

	private final static String TAG = "AppManager";
	private static AppManager instance = null;
	private static  Context context = null;
	private ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); //用来存储获取的应用信息数据

	private AppManager()
	{

	}

	public static AppManager getInstance(Context c)
	{
		if(context != c)
		{
			instance = new AppManager();
			instance.context = c;
			instance.getAppList();
		}

		return instance;
	}

	public int getCount()
	{
		return appList.size();
	}

	public Drawable getAppIcon(int index)
	{
		return appList.get(index).appIcon;
	}


	public String getAppName(int index)
	{
		return appList.get(index).appName;
	}

	public String getAppPackage(int index)
	{
		if(index >= appList.size())
		{
			return null;
		}

		return appList.get(index).packageName;
	}

	private void getAppList()
	{
		boolean isContinue = false;
		List<PackageInfo> packages = context.getPackageManager().getInstalledPackages(0);
		for(int i=0;i<packages.size();i++) 
		{ 
			PackageInfo packageInfo = packages.get(i);
			isContinue = false;

			String hideList[] = {"com.zhongketech.sunflower"};

			if(null == context.getPackageManager().getLaunchIntentForPackage(packageInfo.packageName))
			{
				continue;
			}

			for(int k = 0;k < hideList.length;k++)
			{
				if(hideList[k].equals(packageInfo.packageName))
				{
					isContinue = true;
					break;
				}
			}

			if(isContinue)
			{
				continue;
			}
			
	

			final AppInfo tmpInfo =new AppInfo(); 
			tmpInfo.appName = packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString(); 
			tmpInfo.packageName = packageInfo.packageName; 
			tmpInfo.versionName = packageInfo.versionName; 
			tmpInfo.versionCode = packageInfo.versionCode; 
			tmpInfo.appIcon = packageInfo.applicationInfo.loadIcon(context.getPackageManager());
			//check this app is stop ??
			if(Data.getinstance().isExit(new ApplicationData(tmpInfo.packageName, tmpInfo.appName)))
			{
				tmpInfo.isStop = true;
			}
			else
			{
				tmpInfo.isStop = false;
			}
			
			appList.add(tmpInfo);	
		}
	}



	public boolean setAppInfoStopFlag(String packageName,boolean flag)
	{
		getAppInfo(packageName).isStop = flag;
		if(flag)
		{
			Data.getinstance().add(new ApplicationData(getAppInfo(packageName).packageName, getAppInfo(packageName).appName),Data.DATATYPE_STOPAPP);
		}
		else
		{
			Data.getinstance().del(new ApplicationData(getAppInfo(packageName).packageName, getAppInfo(packageName).appName),Data.DATATYPE_STOPAPP);
		}

		return true;
	}

	public boolean setAppInfoStopFlag(int index,boolean flag)
	{
		if(index >= appList.size())
		{
			return false;
		}

		appList.get(index).isStop = flag;
		if(flag)
		{
			Data.getinstance().add(new ApplicationData(appList.get(index).packageName, appList.get(index).appName),Data.DATATYPE_STOPAPP);
		}
		else
		{
			Data.getinstance().del(new ApplicationData(appList.get(index).packageName, appList.get(index).appName),Data.DATATYPE_STOPAPP);
		}



		return true;
	}


	public AppInfo getAppInfo(int index)
	{
		if(index >= appList.size())
		{
			return null;
		}

		return appList.get(index);
	}

	public AppInfo getAppInfo(String packages)
	{
		for(int i = 0;null != packages && i < appList.size(); i ++)
		{
			if(packages.equals(appList.get(i).packageName))
			{
				return appList.get(i);
			}
		}

		return null;
	}

}
